package com.huawei.demo;

// 用户身份认证
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
// 请求异常类
import com.huaweicloud.sdk.core.exception.ClientRequestException;
// 导入待请求接口的 request 和 response 类
import com.huaweicloud.sdk.organizations.v1.OrganizationsClient;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.DisableTrustedServiceRequest;
import com.huaweicloud.sdk.organizations.v1.model.DisableTrustedServiceResponse;
import com.huaweicloud.sdk.organizations.v1.model.EnableTrustedServiceRequest;
import com.huaweicloud.sdk.organizations.v1.model.EnableTrustedServiceResponse;
import com.huaweicloud.sdk.organizations.v1.model.ListTrustedServicesRequest;
import com.huaweicloud.sdk.organizations.v1.model.ListTrustedServicesResponse;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.TrustedServiceReqBody;
// 日志打印
import com.huaweicloud.sdk.organizations.v1.region.OrganizationsRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrganizationsTrustedServiceManagementDemo {
    private static final Logger logger =
        LoggerFactory.getLogger(OrganizationsTrustedServiceManagementDemo.class.getName());

    public static void main(String[] args) {
        // 创建认证
        String ak = "{your ak string}";
        String sk = "{your sk string}";

        ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk);

        OrganizationsClient organizationsClient =
            OrganizationsClient.newBuilder()
                .withCredential(auth)
                .withRegion(OrganizationsRegion.valueOf("cn-north-4"))
                .build();

        OrganizationsTrustedServiceManagementDemo demo = new OrganizationsTrustedServiceManagementDemo();

        // 1、创建组织
        demo.createOrganization(organizationsClient);
        // 2、查询所属组织信息
        demo.showOrganization(organizationsClient);
        // 3、启用可信服务
        demo.enableTrustedService(organizationsClient);
        // 4、列出组织的可信服务列表
        demo.listTrustedServices(organizationsClient);
        // 5、禁用受信任服务
        demo.disableTrustedService(organizationsClient);
        // 6、删除组织
        demo.deleteOrganization(organizationsClient);

    }

    public void createOrganization(OrganizationsClient organizationsClient) {
        CreateOrganizationRequest createOrganizationRequest = new CreateOrganizationRequest();
        try {
            CreateOrganizationResponse createOrganizationResponse =
                organizationsClient.createOrganization(createOrganizationRequest);
            logger.info(createOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void showOrganization(OrganizationsClient organizationsClient) {
        ShowOrganizationRequest showOrganizationRequest = new ShowOrganizationRequest();
        try {
            ShowOrganizationResponse showOrganizationResponse =
                organizationsClient.showOrganization(showOrganizationRequest);
            logger.info(showOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void deleteOrganization(OrganizationsClient organizationsClient) {
        DeleteOrganizationRequest deleteOrganizationRequest = new DeleteOrganizationRequest();
        try {
            DeleteOrganizationResponse deleteOrganizationResponse =
                organizationsClient.deleteOrganization(deleteOrganizationRequest);
            logger.info(deleteOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void enableTrustedService(OrganizationsClient organizationsClient) {
        EnableTrustedServiceRequest enableTrustedServiceRequest = new EnableTrustedServiceRequest()
            .withBody(new TrustedServiceReqBody().withServicePrincipal("service_principal"));  // service_principal服务主体名称
        try {
            EnableTrustedServiceResponse enableTrustedServiceResponse =
                organizationsClient.enableTrustedService(enableTrustedServiceRequest);
            logger.info(enableTrustedServiceResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void listTrustedServices(OrganizationsClient organizationsClient) {
        ListTrustedServicesRequest listTrustedServicesRequest = new ListTrustedServicesRequest();
        try {
            ListTrustedServicesResponse listTrustedServicesResponse =
                organizationsClient.listTrustedServices(listTrustedServicesRequest);
            logger.info(listTrustedServicesResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void disableTrustedService(OrganizationsClient organizationsClient) {
        DisableTrustedServiceRequest disableTrustedServiceRequest = new DisableTrustedServiceRequest()
            .withBody(new TrustedServiceReqBody().withServicePrincipal("service_principal"));  // service_principal服务主体名称
        try {
            DisableTrustedServiceResponse disableTrustedServiceResponse =
                organizationsClient.disableTrustedService(disableTrustedServiceRequest);
            logger.info(disableTrustedServiceResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    private static void LogError(ClientRequestException e) {
        logger.error("HttpStatusCode: " + e.getHttpStatusCode());
        logger.error("RequestId: " + e.getRequestId());
        logger.error("ErrorCode: " + e.getErrorCode());
        logger.error("ErrorMsg: " + e.getErrorMsg());
    }
}
